<?php
class FieldController extends BaseController
{
	public function findAll()
	{
		$modelId=Input::get('modelId');

		$validator = Validator::make(
		    array('modelId' => $modelId ),
		    array('modelId' => 'required|exists:model,id' )
		);

		if($validator->fails())
		{
			return Response::json( CMSException::$findFieldErr);
		}

		$fieldService=new FieldService;
		$fieldService->setModelId($modelId);
		$fields=$fieldService->findAll();
		return Response::json($fields);
	}

	public function findByOne()
	{
		$fieldId=Input::get('fieldId');
		$fieldService=new FieldService;
		$field=$fieldService->findByOne($fieldId);
		return Response::json($field);

	}

	public function addField()
	{
		$modelId     =Input::get('modelId');
		$fieldName   =Input::get('fieldName');
		$fieldType   =Input::get('fieldType');
		$displayName =Input::get('displayName');


		$validator = Validator::make(
				array('modelId' 		=> $modelId ,
						'fieldName'     => $fieldName ,
						'fieldType'     => $fieldType , 
						'displayName'   => $displayName),
				array('modelId' 	=> 'required|exists:model,id' ,
					'fieldName'     => "required|unique:field,field_name,null,null,model_id,$modelId|alpha_dash",
					'fieldType'     =>'required|exists:type,type_name',
					'displayName'   =>"required|unique:field,display_name,null,null,model_id,$modelId")
		);

		if($validator->fails())
		{
			return Response::json( CMSException::$addFieldErr);
		}
		$fieldService=new FieldService;

		$fieldService->setModelId($modelId);
		$fieldService->setFieldName($fieldName);
		$fieldService->setFieldType($fieldType);
		$fieldService->setDisplayName($displayName);

		$errCode=$fieldService->addField();
		return Response::json($errCode);

	}

	public function deleteField()
	{
		$fieldId   =Input::get('fieldId');

		$validator = Validator::make(
		    array('fieldId' => $fieldId ),
		    array('fieldId' => 'required|exists:field,id' )
		);

		if($validator->fails())
		{
			return Response::json( CMSException::$deleteFieldErr);
		}

		$fieldService =new FieldService;
		$errCode      =$fieldService->deleteField($fieldId);
		return Response::json($errCode);

	}

	public function updateField()
	{
		$fieldId      =Input::get('fieldId');
		$fieldType    =Input::get('fieldType');
		$fieldName    =Input::get('fieldName');
		$displayName  =Input::get('displayName');

		$fieldService =new FieldService;
		$field=$fieldService->findByOne($fieldId);
		if($field['errCode']==10)
		{
			return Response::json( CMSException::$updateFieldErr);
		}
		if($fieldName==$field->field_name)
		{
			
			if($displayName==$field->display_name)
			{
				$validator = Validator::make(
				    array('fieldType' => $fieldType),
				    array('fieldType'=>'required|exists:type,type_name')
				);
			}
			else
			{
				$validator = Validator::make(
				    array('fieldType' => $fieldType,'displayName' => $displayName),
				    array('fieldType'=>'required|exists:type,type_name','displayName' =>'required|unique:field,display_name')
				);
			}
		}
		else
		{
			if($displayName==$field->display_name)
			{
				$validator = Validator::make(
				    array('fieldName' => $fieldName,'fieldType' => $fieldType),
				    array('fieldName'=>'required|unique:field,field_name|alpha_dash','fieldType'=>'required|exists:type,type_name')
				);
			}
			else
			{
				$validator = Validator::make(
				    array('fieldName' => $fieldName,'fieldType' => $fieldType,'displayName' => $displayName),
				    array('fieldName'=>'required|unique:field,field_name|alpha_dash','fieldType'=>'required|exists:type,type_name','displayName' =>'required|unique:field,display_name')
				);
			}

		}


		if($validator->fails())
		{
			return Response::json( CMSException::$updateFieldErr);
		}

		$fieldService =new FieldService;
		$fieldService->setFieldName($fieldName);
		$fieldService->setFieldType($fieldType);
		$fieldService->setDisplayName($displayName);

		$errCode=$fieldService->updateField($fieldId);
		return Response::json($errCode);
	}
}
?>